package com.chinapost.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.DeptServiceI;
import com.chinapost.service.WdxxServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Wdxx;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "wdxxAction", results = { 
		@Result(name = "wdxxManager", location = "/jsp/wdxx/wdxxManager.jsp"),
		@Result(name = "wdxxAdd", location = "/jsp/wdxx/wdxxAdd.jsp"), 
		@Result(name = "wdxxMap", location = "/jsp/wdxx/wdxxMap.jsp"), 
		@Result(name = "wdxxMaps", location = "/jsp/wdxx/wdxxMaps.jsp"), 
		@Result(name = "wdxxImport", location = "/jsp/wdxx/wdxxImport.jsp"),
		@Result(name = "wdxxEdit", location = "/jsp/wdxx/wdxxEdit.jsp") 
		})
public class WdxxAction extends BaseAction implements ModelDriven<Wdxx>, ServletRequestAware {

	private Wdxx wdxx = new Wdxx();
	private WdxxServiceI wdxxService;
	private DeptServiceI deptService;
	private HttpServletRequest request;

	public DeptServiceI getDeptService() {
		return deptService;
	}

	@Autowired
	public void setDeptService(DeptServiceI deptService) {
		this.deptService = deptService;
	}

	public WdxxServiceI getWdxxService() {
		return wdxxService;
	}

	@Autowired
	public void setWdxxService(WdxxServiceI wdxxService) {
		this.wdxxService = wdxxService;
	}

	@Override
	public Wdxx getModel() {
		return wdxx;
	}

	public String wdxxManager() {
		return "wdxxManager";
	}

	public String wdxxEdit() {
		return "wdxxEdit";
	}

	public String wdxxAdd() {
		return "wdxxAdd";
	}

	public String wdxxImport() {
		return "wdxxImport";
	}

	public String wdxxMap() {
		if (!StringUtils.isEmpty(wdxx.getCid())) {
			Wdxx data = wdxxService.getWdxxById(wdxx.getCid());
			request.setAttribute("data", data);
		}
		return "wdxxMap";
	}
	
	public String wdxxMaps() {
		return "wdxxMaps";
	}
	

	public void add() {
		Json j = new Json();
		try {
			wdxxService.add(wdxx);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	public void addWdxxs() {
		Json j = new Json();
		int count = 0;
		try {
			count = wdxxService.addWdxxs(wdxx);
			if (count > 0) {
				j.setSuccess(true);
				j.setMsg("上传成功，新增" + count + "条网点信息记录！");
			} else {
				j.setMsg("上传失败,新增0条记录！");
			}
		} catch (Exception e) {
			j.setMsg("上传失败,新增0条记录！");
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			wdxxService.edit(wdxx);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		try {
			wdxxService.delete(wdxx);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(wdxx.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	public void datagrid() {
		if (!StringUtils.isEmpty(wdxx.getDeptId())) {
			wdxx.setDeptIds(deptService.getChildIds(wdxx.getDeptId()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			wdxx.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(wdxxService.datagrid(wdxx));
	}
	
	public void do_combobox(){
		List<Wdxx> list = new ArrayList<Wdxx>();
		if (wdxx.getDeptId()!=null){
			list = wdxxService.listWdxx(wdxx.getDeptId());
		}
		super.writeJson(list);
	}
	
	public void getWdxxs(){
		List<Wdxx> list = new ArrayList<Wdxx>();
		if (wdxx.getDeptId()!=null){
			wdxx.setDeptIds(deptService.getChildIds(wdxx.getDeptId()));
			list = wdxxService.findWdxxs(wdxx);
		}
		super.writeJson(list);
	}
	

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
